package cn.edu.neusoft.food229.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.Toast;

import cn.edu.neusoft.food229.R;
import cn.edu.neusoft.food229.bean.LoginBean;
import cn.edu.neusoft.food229.listener.LoginListener;
import cn.edu.neusoft.food229.model.LoginModel;

public class LoginActivity extends AppCompatActivity {
    private EditText ed1,ed2;
    private Button bt2,bt3;
    private Switch sw;
    private String username ="", password ="", userid ="";
    private SharedPreferences sp;

    private LoginListener loginListener=new LoginListener() {
        @Override
        public void onResponce(LoginBean loginBean) {
            userid=loginBean.getUserid();
            Log.i("LoginActivity","--------userid=="+userid);
            if(userid!=null){
                if(userid!="0"){
//                判断开关存密码
                if(sw.isChecked()){
                    saveData(ed1.getText().toString(),ed2.getText().toString(),userid,sw.isChecked());
                }
                else{
                    saveData("","","",false);
                }
                Toast.makeText(LoginActivity.this, "   登   录  成  功   "+"id=="+userid,Toast.LENGTH_SHORT).show();
                Intent intent=new Intent(LoginActivity.this,MainActivity.class);
                startActivity(intent);
                finish();
            }

            else {
                Toast.makeText(LoginActivity.this, "登录失败", Toast.LENGTH_SHORT).show();
                saveData("","","",false);
               }
           }
        }

        @Override
        public void onFail(String msg) {
            saveData("","","",false);
            Toast.makeText(LoginActivity.this,msg,Toast.LENGTH_SHORT).show();

        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initViews();
        sp=getSharedPreferences("login",MODE_PRIVATE);
        readData();
    }
    private void saveData(String name, String pass, String userid, Boolean state){
        SharedPreferences.Editor editor=sp.edit();
        editor.putString("username",name);
        editor.putString("userpass",pass);
        editor.putBoolean("state",state);
        editor.putString("userid",userid);
        editor.commit();
    }


    private void initViews() {
        ed1=findViewById(R.id.editText3);
        ed2=findViewById(R.id.editText4);
        bt2=findViewById(R.id.button1);
        bt3=findViewById(R.id.button7);
        sw=findViewById(R.id.switch2);
        bt2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                username=ed1.getText().toString();
                password=ed2.getText().toString();

                String un=ed1.getText().toString();
                String um=ed2.getText().toString();
                if(TextUtils.isEmpty(un) || TextUtils.isEmpty(um))
                {
                    Toast.makeText(LoginActivity.this,"不能有空项,登陆失败 ", Toast.LENGTH_SHORT).show();
                    return;
                }
                else {
                    Toast.makeText(LoginActivity.this, "用户名:"+un , Toast.LENGTH_SHORT).show();
                    LoginModel loginModel=new LoginModel();
                    loginModel.getLoginResult(username,password,loginListener);
                }
            }

        });
        bt3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(LoginActivity.this, RegtisterActivity.class);
               // startActivity(intent);//跳转
                startActivityForResult(intent,1);
                Toast.makeText(LoginActivity.this, "注册", Toast.LENGTH_SHORT).show();

            }
        });
    }


    private void readData(){
    String unsme=sp.getString("username","");
    String pass=sp.getString("userpass","");
    String userId=sp.getString("user_id", "");
    Boolean state=sp.getBoolean("state",false);
    if(state){
        ed1.setText(unsme);
        ed2.setText(pass);
    }
        sw.setChecked(state);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        super.onActivityResult(requestCode, resultCode, data);
        String et_user= data.getStringExtra("name");
        ed1.setText(et_user);
        String et_userpass= data.getStringExtra("password");
        ed2.setText(et_userpass);
    }
}
